#!/bin/sh
#	$OpenBSD: regress.subr,v 1.4 2017/03/14 20:22:06 eric Exp $

set -e

fail()
{
	echo "*** ERROR: $@"
	exit 1
}

regress()
{
	local cmd="$1"
	local bin0="/bin/${cmd}${A}"
	local bin1="/bin/${cmd}${B}"
	local out="${OUTDIR}/test-${COUNT}.out"
	local out0="${OUTDIR}/test-${COUNT}${A}.out"
	local out1="${OUTDIR}/test-${COUNT}${B}.out"
	local err0="${OUTDIR}/test-${COUNT}${A}.err"
	local err1="${OUTDIR}/test-${COUNT}${B}.err"
	local outdiff="${OUTDIR}/test-${COUNT}${A}${B}.diff"
	shift

	test -x "${ROOTDIR}${bin0}" || fail "${ROOTDIR}${bin0}" not executable
	test -x "${ROOTDIR}${bin1}" || fail "${ROOTDIR}${bin1}" not executable

	echo -n "${cmd} $@ ."

	set +e
	chroot -u bin "${ROOTDIR}" ${bin0} $@ > ${out0}
	echo -n .
	chroot -u bin "${ROOTDIR}" ${bin1} $@ > ${out1}
	echo -n "."

	diff -u ${out0} ${out1} > ${outdiff}
	set -e
	if test -s ${outdiff}; then
		FAIL=$((FAIL+1))
		echo fail
		echo "### FAIL [#${COUNT} env=${REGRESSENV}] ${cmd} $@" >> $ERR
		cat ${outdiff} >> $ERR
		echo >> $ERR
	else
		rm ${outdiff}
		rm ${out1}
		mv ${out0} ${out}
		OK=$((OK+1))
		echo ok
		echo "### OK [#${COUNT} env=$REGRESSENV] ${cmd} $@" >> $OUT
		cat ${out} >> $OUT
		echo >> $OUT
	fi

	COUNT=$((COUNT+1))
}

regress_setenv()
{
	local _name="$1"

	echo "==> using env $_name"

	mkdir -p "${ROOTDIR}/etc/"
	cp /etc/hosts "${ROOTDIR}/etc/"
	cp /etc/resolv.conf "${ROOTDIR}/etc/"
	cp /etc/protocols "${ROOTDIR}/etc/"
	cp /etc/networks "${ROOTDIR}/etc/"
	cp /etc/services "${ROOTDIR}/etc/"

	case $_name in
	empty)
		rm -f "${ROOTDIR}/etc/*"
		;;
	local)
		;;
	file)
		grep -v lookup /etc/resolv.conf > "${ROOTDIR}/etc/resolv.conf"
		echo "lookup file" >> "${ROOTDIR}/etc/resolv.conf"
		;;
	bind)
		grep -v lookup /etc/resolv.conf > "${ROOTDIR}/etc/resolv.conf"
		echo "lookup bind" >> "${ROOTDIR}/etc/resolv.conf"
		;;
	*)
		fail unknown env $_name
		;;
	esac
	REGRESSENV=$_name
}

regress_digest()
{
	echo "===> done"
 	test -f $ERR && (echo; cat $ERR; echo "===> errfile=$ERR")
	echo "===> logfile=$OUT"
	echo "===> run=$COUNT ok=$OK fail=$FAIL error=$ERROR"

}

# needed for chroot
test "$(id -u)" -ne 0 && fail need root privileges to run this script

# we really really want to avoid erasing /etc later
test "${RUNDIR}" || fail RUNDIR is not set

ROOTDIR=$(readlink -fn ${RUNDIR})
test "${ROOTDIR}" == "/" && fail RUNDIR is root dir: ${RUNDIR}

COUNT=0
OK=0
FAIL=0
ERROR=0
A=${A:=.a}
B=${B:=.b}

OUTDIR=$(mktemp -d -p $ROOTDIR)
chmod a+rx ${OUTDIR}
echo "===> ${OUTDIR}"

OUT=$OUTDIR/regress.log
ERR=$OUTDIR/regress.err
